<?php
function UnionOpenGoodsQueryRequest($keyword){
    require (app_path() . "/Libs/jd/config.php");//apikey配置文件
    require (app_path() . "/Libs/jd/JdClient.php");//请求文件
    require (app_path() . "/Libs/jd/RequestCheckUtil.php");//请求文件
    require (app_path() . "/Libs/jd/request/UnionOpenGoodsQueryRequest.php");//接口参数
    $AppKey=$ApiConfig["AppKey"];
    $AppSecret=$ApiConfig["AppSecret"];

    $c = new \JdClient();
    $c->appKey = $ApiConfig["AppKey"];
    $c->appSecret = $ApiConfig["AppSecret"];

    $req = new \UnionOpenGoodsQueryRequest();
    $goodsReqDTO= array();
    // $goodsReqDTO["keyword"]="https://u.jd.com/HCf9qZ9";
    $goodsReqDTO["keyword"]=$keyword;

    $req->setGoodsReqDTO($goodsReqDTO);
    $resp = $c->execute($req) -> jd_union_open_goods_query_responce -> queryResult;

    return $resp;//string
}
function promotion_byunionid_get($unionId,$positionid,$url){
    require (app_path() . "/Libs/jd/config.php");//apikey配置文件
    require (app_path() . "/Libs/jd/JdClient.php");//请求文件
    require (app_path() . "/Libs/jd/RequestCheckUtil.php");//请求文件
    require (app_path() . "/Libs/jd/request/UnionOpenPromotionByunionidGetRequest.php");//接口参数
    $AppKey=$ApiConfig["AppKey"];
    $AppSecret=$ApiConfig["AppSecret"];

    $c = new \JdClient();
    $c->appKey = $AppKey ;
    $c->appSecret = $AppSecret;
    $res = new \UnionOpenPromotionByunionidGetRequest();
    $promotionCodeReq= array();
    $promotionCodeReq["materialId"] = $url;//推广物料url，例如活动链接、商品链接等；不支持仅传入skuid
    $promotionCodeReq["unionId"] = $unionId;//联盟id,Number
    $promotionCodeReq["positionId"] = $positionid;//推广位id,Number
    $res -> setPromotionCodeReq($promotionCodeReq);
    $resp = $c->execute($res) -> jd_union_open_promotion_byunionid_get_responce;
    return $resp -> getResult;

    //{"code":200,"data":{"shortURL":"https://u.jd.com/BIUP1cW"},"message":"success","requestId":"o_0b648113_l0davi7j_64445584"}
    //{"code":2001703,"message":"您填写的原始链接materialId不符合要求！","requestId":"o_0b73145e_l0daw19j_64402262"}
}

function mtw_url($url){
	$curl = curl_init();
	$url = urlencode($url);
	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'http://api.suowo.cn/api.htm?url='.$url.'&format=json&key=5cf2495eb1a9c76c94e7fabd@2e3ebc3e09a09fc5f3346d91c09bd2f2&expireDate=2030-03-31',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	  CURLOPT_HTTPHEADER => array(
	    'Cookie: sitename=8b8e996e1e5249ddb16ad09ae54d0c3d'
	  ),
	));

	$response = curl_exec($curl);

	curl_close($curl);
	// echo $response;
	// var_dump(json_decode($response,true));//array(2) { ["err"]=> string(0) "" ["url"]=> string(20) "http://mtw.so/5UIxbI" }
	$result = json_decode($response,true);
	if($result["err"] == ""){
		// return "返回空，转链正常：".$result["url"];
		return $result["url"];
	}else{
		return "error:转链错误信息：".$result["err"];
	}
}

function curl_get_https($url){
		$curl = curl_init(); // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
		//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在
		$tmpInfo = curl_exec($curl);     //返回api的json对象
		//关闭URL请求
		curl_close($curl);
		return $tmpInfo;    //返回json对象
	}
function extract_jdunionurl($str){
	//遍历取u.jd.com
	preg_match_all("/https:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/",$str,$arr);
	//匹配不到优惠券地址，如果匹配不到u.jd.com就用这个匹配,提取主图
	$url_arr = array();
	$num = count($arr[0]);
	for($i=0; $i < $num; $i++){
		if(strstr($arr[0][$i], 'u.jd.com')){
			$url = $arr[0][$i];
			$url = str_replace("\n","",$url);
			$url_arr[] = $url;
		}
	}

	return $url_arr;
}
